
package com.geek.bams.client;

import com.geek.bams.entity.Account;
import com.geek.bams.service.Bank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.Socket;
//MyFrame -> Container（卡片式布局方式） -> JPanel -> JButton、JTextField...组件
/**
 * ATMClient是JFrame，自己写的。一般Frame自己搭，Panel拖拽
 * JPanel，就是拖拽的，通过先创建.jfd文件，自动生成对应的Panel.java文件
 */

public class ATMClient extends JFrame {
    //JPanel界面
    MainPanel mainPanel;
    LoginPanel loginPanel;
    RegisterPanel registerPanel;
    BusinessPanel businessPanel;
    //共同资源
    Bank bank;
    Account account;

    //网络编程的资源
    Socket socket;
    PrintWriter pw;
    ObjectInputStream ois;

    //实例化
    public ATMClient() {
        //设置窗体的基本信息
        //设置窗体的标题
        this.setTitle("ATM Client");
        //设置窗体的宽高
        //this.setSize(400, 500);
        this.setSize(500,650);
        //设置窗体的关闭方式  关闭即退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置不可拖拽
        this.setResizable(false);
        //设置窗体的居中显示
        this.setLocationRelativeTo(null);
        //初始化共同数据
        bank  = Bank.getInstance();

        //运行ATMClient客户端，实例化网络编程的资源
        try {
            socket = new Socket("127.0.0.1" , 8899);
            //socket = new Socket("192.168.0.235" , 8899);
            pw = new PrintWriter(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
        } catch (ConnectException e) {
            JOptionPane.showMessageDialog(this , "服务器未启动");
            System.exit(0);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //给客户端添加关闭事件
        //关闭客户端 - 添加窗口关闭的事件
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //发送关闭close客户端的指令
                //发送指令 PrintWriter 关闭指令close
                pw.println("close");
                pw.flush();

                //空读处理
                try {
                    //读 空数据
                    ois.readObject();
                    //关闭客户端资源
                    socket.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null , "客户端已关闭！");
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }

                System.exit(0);
            }
        });

        //实例化JPanel界面（添加JPanel组件）
        mainPanel = new MainPanel(this);
        loginPanel = new LoginPanel(this);
        registerPanel = new RegisterPanel(this);
        businessPanel = new BusinessPanel(this);

        //将JPanel添加至JFrame窗体容器中
        Container container = this.getContentPane();
        //设置容器为卡片式布局
        container.setLayout(new CardLayout());
        //默认显示第一个
        container.add(mainPanel ,"mainPanel");
        container.add(loginPanel ,"loginPanel");
        container.add(registerPanel ,"registerPanel");
        container.add(businessPanel ,"businessPanel");

    }

    public static void main(String[] args) {

        new ATMClient().setVisible(true);
    }
}
